package org.gameis.furtal.client;

import org.gameis.furtal.FurtalConstants;
import org.gameis.furtal.utils.FurtalUtils;

import javax.net.SocketFactory;
import java.io.*;
import java.net.Socket;

/**
 * Created by IntelliJ IDEA.
 * User: zvoykish
 * Date: 27/1/12
 * Time: 12:53 PM
 */
public class FurtalClient {
    public static final String HOSTNAME = "127.0.0.1";

    public void start() throws IOException {
        Socket socket = SocketFactory.getDefault().createSocket(HOSTNAME, FurtalConstants.SERVER_PORT);
        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();

        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
        PrintWriter out = new PrintWriter(outputStream);
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

        out.print("Hello");
        String fromServer;
        while ((fromServer = in.readLine()) != null) {
            FurtalUtils.print("Server: " + fromServer);
            if (fromServer.equals("Bye.")) {
                break;
            }
            String fromUser = stdIn.readLine();
            if (fromUser != null) {
                FurtalUtils.print
                        ("Client: " + fromUser);
                out.println(fromUser);
            }
        }

//        writer.print(FurtalConstants.HELLO_MESSAGE);
        socket.close();
    }

    public static void main(String[] args) {
        FurtalClient client = new FurtalClient();
        try {
            client.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
